package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.pojo.CheckItem;

import java.util.List;

@Transactional
@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImp implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        //完成分页查询,基于mybatis框架的分页助手
        PageHelper.startPage(currentPage,pageSize);
        if(queryString=="null"||queryString==null){
            queryString="";
        }
        Page<CheckItem> page = checkItemDao.selectByCondition("%"+queryString+"%");
        List<CheckItem> rows = page.getResult();
        long total = page.getTotal();
        return new PageResult(total,rows);
    }

    //根据id删除检查项
    @Override
    public void deleteById(Integer id) {
        //判断当前检查项已经关联检查组了
        long count = checkItemDao.findCountByCheckItemId(id);
        if(count>0){
            new RuntimeException();
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> list=checkItemDao.findAll();
        return list;
    }
}
